package com.block.merge.sdk;

import android.util.Log;

import com.block.merge.sdk.ad.BlocklVideoIAP;
import com.block.merge.sdk.bridge.BlockCocosBridge;
import com.block.merge.sdk.manager.BlockVideoManger;
import com.block.merge.sdk.max.BlocklMaxReward;

import org.json.JSONObject;

public class BlockAdSdk {

    public static void showBiddingAd(String ad_source) {
        showAd(new VideoRewardCallback() {

            @Override
            public void onAdOver() {
                adCallBack("ad_over_w",ad_source);
            }

            @Override
            public void onAdError() {
                BlockCocosBridge.getInstance().XSSdkCallback("ad_error_w", "{}");
            }
        });
    }

    private static void adCallBack(String state,String ad_source) {
        try {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("ad_source",ad_source);
            BlockCocosBridge.getInstance().XSSdkCallback(state, jsonObject.toString());
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    private static void showAd(VideoRewardCallback callback) {
        BlocklVideoIAP service = null;
        double ecpm1 =  BlocklMaxReward.getInstance().getEcpm();
        if (ecpm1>=0){
            service= BlockVideoManger.getReward1Instance();
            Log.e("BloMerge","show max reward");
        }

        if (service == null) {
            callback.onAdError();
            return;
        }

        try {
            service.showVideo(BlockCocosBridge.mainGameActivity.get(), new OnAdShowListner() {
                @Override
                public void reward(boolean is ) {
                    try {
                        if (callback != null) {
                            callback.onAdOver();
                        }
                    } catch (Exception e) {
                    }
                }

                @Override
                public void error(String errorCode) {
                    try {
                        if (callback != null) {
                            callback.onAdError();
                        }
                    } catch (Exception e) {
                    }
                }
            });
        } catch (Exception e) {
            try {
                callback.onAdError();
            } catch (Exception e1) {
            }
        }
    }

    public interface VideoRewardCallback {

        void onAdOver();

        void onAdError();
    }

    public interface OnAdShowListner {
        void reward(boolean is);

        void error(String errorCode);
    }

}
